

package com.wake.widget.skin;

import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.wake.widget.NMUILog;
import com.wake.widget.R;
import com.wake.widget.skin.defaultAttr.INMUISkinDefaultAttrProvider;
import com.wake.widget.util.NMUIResHelper;

public class NMUISkinHelper {

    public static NMUISkinValueBuilder sSkinValueBuilder = NMUISkinValueBuilder.acquire();

    public static Resources.Theme getSkinTheme(@NonNull View view) {
        NMUISkinManager.ViewSkinCurrent current = NMUISkinManager.getViewSkinCurrent(view);
        Resources.Theme theme;
        if (current == null || current.index < 0) {
            theme = view.getContext().getTheme();
        } else {
            theme = NMUISkinManager.of(current.managerName, view.getContext()).getTheme(current.index);
        }
        return theme;
    }

    public static int getSkinColor(@NonNull View view, int colorAttr) {
        return NMUIResHelper.getAttrColor(getSkinTheme(view), colorAttr);
    }

    public static ColorStateList getSkinColorStateList(@NonNull View view, int colorAttr) {
        return NMUIResHelper.getAttrColorStateList(view.getContext(), getSkinTheme(view), colorAttr);
    }

    @Nullable
    public static Drawable getSkinDrawable(@NonNull View view, int drawableAttr) {
        return NMUIResHelper.getAttrDrawable(view.getContext(), getSkinTheme(view), drawableAttr);
    }


    public static void setSkinValue(@NonNull View view, NMUISkinValueBuilder skinValueBuilder) {
        setSkinValue(view, skinValueBuilder.build());
    }

    public static void setSkinValue(@NonNull View view, String value) {
        view.setTag(R.id.nmui_skin_value, value);
        refreshViewSkin(view);

    }

    @MainThread
    public static void setSkinValue(@NonNull View view, SkinWriter writer) {
        writer.write(sSkinValueBuilder);
        setSkinValue(view, sSkinValueBuilder.build());
        sSkinValueBuilder.clear();
    }

    public static void refreshRVItemDecoration(@NonNull RecyclerView view, INMUISkinHandlerDecoration itemDecoration) {
        NMUISkinManager.ViewSkinCurrent skinCurrent = NMUISkinManager.getViewSkinCurrent(view);
        if (skinCurrent != null) {
            NMUISkinManager.of(skinCurrent.managerName, view.getContext()).refreshRecyclerDecoration(view, itemDecoration, skinCurrent.index);
        }
    }

    public static int getCurrentSkinIndex(@NonNull View view) {
        NMUISkinManager.ViewSkinCurrent viewSkinCurrent = NMUISkinManager.getViewSkinCurrent(view);
        if (viewSkinCurrent != null) {
            return viewSkinCurrent.index;
        }
        return NMUISkinManager.DEFAULT_SKIN;
    }

    public static void refreshViewSkin(@NonNull View view) {
        NMUISkinManager.ViewSkinCurrent skinCurrent = NMUISkinManager.getViewSkinCurrent(view);
        if (skinCurrent != null) {
            NMUISkinManager.of(skinCurrent.managerName, view.getContext()).refreshTheme(view, skinCurrent.index);
        }
    }

    public static void syncViewSkin(@NonNull View view, @NonNull View sourceView) {
        NMUISkinManager.ViewSkinCurrent source = NMUISkinManager.getViewSkinCurrent(sourceView);
        if (source != null) {
            NMUISkinManager.ViewSkinCurrent skin = NMUISkinManager.getViewSkinCurrent(view);
            if (!source.equals(skin)) {
                NMUISkinManager.of(source.managerName, view.getContext()).dispatch(view, source.index);
            }
        }
    }

    public static void setSkinDefaultProvider(@NonNull View view,
                                              INMUISkinDefaultAttrProvider provider) {
        view.setTag(R.id.nmui_skin_default_attr_provider, provider);
    }

    public static void setSkinApplyListener(@NonNull View view, @Nullable INMUISkinApplyListener listener) {
        view.setTag(R.id.nmui_skin_apply_listener, listener);
    }

    @Nullable
    public static INMUISkinApplyListener getSkinApplyListener(@NonNull View view) {
        Object listener = view.getTag(R.id.nmui_skin_apply_listener);
        if (listener instanceof INMUISkinApplyListener) {
            return (INMUISkinApplyListener) listener;
        }
        return null;
    }

    public static void setIgnoreSkinApply(@NonNull View view, boolean ignore){
        view.setTag(R.id.nmui_skin_ignore_apply, ignore);
    }

    public static void setInterceptSkinDispatch(@NonNull View view, boolean intercept){
        view.setTag(R.id.nmui_skin_intercept_dispatch, intercept);
    }

    public static void warnRuleNotSupport(View view, String rule) {
        NMUILog.w("NMUISkinManager",
                view.getClass().getSimpleName() + " does't support " + rule);
    }
}
